package com.iconmaker.controller;

import java.io.File;
import java.io.FilenameFilter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bckim.iconmaker.R;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.iconmaker.model.Conf;
import com.iconmaker.model.State;

public class GalleryActivity extends Activity implements OnItemClickListener, android.view.View.OnClickListener {
	private String[] imageList;
	private GridAdapter gridAdapter;
	private GridView gridView;
	private static final int REQUEST_CODE_LOAD_IMAGE = 101;
	private static Activity context;
	private AdView adView;
	
	@Override
	protected void onDestroy() {
		if(adView != null) {
			adView.destroy();
		}
		
		super.onDestroy();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		startActivity(new Intent(this, IntroActivity.class));
		
		setContentView(R.layout.activity_gallery);
		
		State.galleryMode = State.GALLERY_MODE_SELECT;
		context = this;
		
		String rootPath = Conf.rootPath + this.getPackageName();
		File file = new File(rootPath);
		imageList = null;
		
		if(file.exists()) {
			imageList = file.list(new FilenameFilter() {
				
				@Override
				public boolean accept(File dir, String filename) {
					return true;
				}
			});
		}
		
		gridView = (GridView)findViewById(R.id.gridView);
		gridAdapter = new GridAdapter(this, imageList);
		gridView.setAdapter(gridAdapter);
		
		gridView.setOnItemClickListener(this);
		((TextView)findViewById(R.id.btn_edit)).setOnClickListener(this);
		
		//admob
		adView = new AdView(this, AdSize.BANNER, "a153140fc2490a2");
		((LinearLayout)findViewById(R.id.ad_container)).addView(adView);
		adView.loadAd(new AdRequest());
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		final Intent intent = new Intent(this, CanvasActivity.class);
		if(position == 0) {
			CharSequence[] option = new CharSequence[2];
			option[0] = "16 * 16";
			option[1] = "32 * 32";
			
			new AlertDialog.Builder(context).setSingleChoiceItems(option, 0, null).setPositiveButton(android.R.string.ok, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
					
					if(selectedPosition == 0) {
						Conf.countOfColumnPixcel = 16;
						Conf.countOfRowPixcel = 16;
					}
					else {
						Conf.countOfColumnPixcel = 32;
						Conf.countOfRowPixcel = 32;
					}
					
					intent.putExtra(CanvasActivity.KEY_LOAD_IMAGE_NAME, "new");
					startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);
				}
			}).setNegativeButton(android.R.string.cancel, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).setTitle("Please select the image size.").show();
			
		}
		else {
			if(State.galleryMode == State.GALLERY_MODE_REMOVE) {
				DataManager.remove(imageList[position - 1]);
				refresh();
			}
			else if(State.galleryMode == State.GALLERY_MODE_SELECT) {
				intent.putExtra(CanvasActivity.KEY_LOAD_IMAGE_NAME, imageList[position - 1]);
				startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
	}
	
	@Override
	protected void onRestart() {
		refresh();
		
		super.onRestart();
	}
	
	private void refresh() {
		String rootPath = Conf.rootPath + this.getPackageName();
		File file = new File(rootPath);
		
		if(file.exists()) {
			imageList = file.list(new FilenameFilter() {
				
				@Override
				public boolean accept(File dir, String filename) {
					if(filename.contains(".png"))
						return true;
					
					return false;
				}
			});
		}
		gridAdapter.setDataList(imageList);
		gridAdapter.notifyDataSetChanged();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.btn_edit :
				int visible;
				
				if(State.galleryMode == State.GALLERY_MODE_SELECT) {
					((TextView)v).setText("Done");
					visible = View.VISIBLE;
					State.galleryMode = State.GALLERY_MODE_REMOVE;
				}
				else {
					((TextView)v).setText("Edit");
					visible = View.GONE;
					State.galleryMode = State.GALLERY_MODE_SELECT;
				}
				
				for(int i = 1; i < gridView.getChildCount(); i++) {
					gridView.getChildAt(i).findViewById(R.id.image_delete).setVisibility(visible);
				}
				break;
		}
	}
	
	public static Activity getContext() {
		return context;
	}
}
